/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import constant.CourseTypeEnum;
import java.awt.Frame;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.CourseGrade;
import model.Grades;
import model.Person;

/**
 *
 * @author 乐山
 */
public class StuCoursesInfoDialog extends javax.swing.JDialog {
    private Person person;

    public StuCoursesInfoDialog(Frame owner, boolean modal) {
        super(owner, modal);
        initComponents();
    }
    
    
    
    public StuCoursesInfoDialog(java.awt.Frame parent, boolean modal, Person person) {
        super(parent, modal);
        initComponents();
        this.person = person;
        this.setTitle(person.getClazz()+"班"+person.getName()+"修习信息");
        initAllTable(person);
    }
    
    private void initAllTable(Person person) {
        
        Grades grades = person.getGrade();
        
        //基础选修
        DefaultTableModel basicdtm = (DefaultTableModel)basicECTable.getModel();
        basicdtm.setRowCount(0);
        List<CourseGrade> basicEC = grades.getBasicEC();
        Object[][] basicStudyInfo = generateStudyInfo(basicEC);
        for (int i = 0; i < basicStudyInfo.length; i++) {
            basicdtm.addRow(basicStudyInfo[i]);
        }
        
        //专业选修
        DefaultTableModel speicaldtm = (DefaultTableModel)specialECTable.getModel();
        speicaldtm.setRowCount(0);
        List<CourseGrade> specialEC = grades.getSpecialEC();
        Object[][] specialECStudyInfo = generateStudyInfo(specialEC);
        for (int i = 0; i < specialECStudyInfo.length; i++) {
            speicaldtm.addRow(specialECStudyInfo[i]);
        } 
    }
    
    private Object[][] generateStudyInfo(List<CourseGrade> cgList) {
        Object[][] result = new Object[cgList.size()][5];
        for (int i = 0; i < cgList.size(); i++) {
            result[i][0] = cgList.get(i).getName();
            result[i][1] = cgList.get(i).getType();
            result[i][2] = cgList.get(i).getExamStat();
            result[i][3] = cgList.get(i).getCredit();
            result[i][4] = cgList.get(i).getFinalGrade();
        }
        return result;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        basicECTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        specialECTable = new javax.swing.JTable();
        analyzeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "基础选修", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("宋体", 1, 14))); // NOI18N

        basicECTable.setFont(new java.awt.Font("宋体", 1, 12)); // NOI18N
        basicECTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "课程名称", "修习类别", "考试类别", "学分", "最终成绩"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(basicECTable);
        if (basicECTable.getColumnModel().getColumnCount() > 0) {
            basicECTable.getColumnModel().getColumn(0).setMinWidth(120);
        }

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 2, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "专业选修", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("宋体", 1, 14))); // NOI18N

        specialECTable.setFont(new java.awt.Font("宋体", 1, 12)); // NOI18N
        specialECTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "课程名称", "修习类别", "考试类别", "学分", "最终成绩"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(specialECTable);
        if (specialECTable.getColumnModel().getColumnCount() > 0) {
            specialECTable.getColumnModel().getColumn(0).setMinWidth(120);
        }

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        analyzeButton.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        analyzeButton.setText("分析");
        analyzeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                analyzeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(126, 126, 126)
                .addComponent(analyzeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(123, 123, 123))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(analyzeButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void analyzeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_analyzeButtonActionPerformed
        Person p = person;
        StringBuilder sb = new StringBuilder();
        sb.append(p.getClazz()+"班 "+p.getName()+" 学号: "+p.getId()+" \n");
        Grades grade = person.getGrade();
        if (grade.isAllPassed()){
            sb.append("所有修习类别(基础选修、专业选修、专业必修)均已通过");
            JOptionPane.showMessageDialog(this, sb.toString());
            return;
        }
        if(!grade.isPassed(CourseTypeEnum.BASICEC)){
            sb.append("基础选修").append(CourseTypeEnum.BASICEC.getCredit()).append("学分。已获得:")
                    .append(grade.calcBasicECCredit()).append("学分。有如下科目未通过:\n");
            Map<String, Double> failedBasicEC = grade.getFailedBasicEC();
            for (Map.Entry<String, Double> entry : failedBasicEC.entrySet()) {
                String key = entry.getKey();
                Double val = entry.getValue();
                sb.append(String.format("科目:%s(%s)\n", key, val));
            }
        }
        sb.append("\n");
        if(!grade.isPassed(CourseTypeEnum.SPECIALEC)){
            sb.append("专业选修").append(CourseTypeEnum.SPECIALEC.getCredit()).append("学分。已获得:")
                    .append(grade.calcSpecialECCredit()).append("学分。有如下科目未通过:\n");
            Map<String, Double> failedSpeicalEC = grade.getFailedSpecialEC();
            for (Map.Entry<String, Double> entry : failedSpeicalEC.entrySet()) {
                String key = entry.getKey();
                Double val = entry.getValue();
                sb.append(String.format("科目:%s(%s)\n", key, val));
            }
        }
        sb.append("\n");
        if(!grade.isPassed(CourseTypeEnum.SPECIALRC)){
            sb.append("专业必修").append(CourseTypeEnum.SPECIALRC.getCredit()).append("学分。已获得")
               .append(grade.calcSpeicalRCCredit()).append("学分。有如下科目未通过:\n");
            Map<String, Double> failedSpeicalRC = grade.getFailedSpecialRC();
            for (Map.Entry<String, Double> entry : failedSpeicalRC.entrySet()) {
                String key = entry.getKey();
                Double val = entry.getValue();
                sb.append(String.format("科目: %s(%s)\n", key, val));
            }
        }
        JOptionPane.showMessageDialog(this, sb.toString());
    }//GEN-LAST:event_analyzeButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StuCoursesInfoDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StuCoursesInfoDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StuCoursesInfoDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StuCoursesInfoDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                StuCoursesInfoDialog dialog = new StuCoursesInfoDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton analyzeButton;
    private javax.swing.JTable basicECTable;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable specialECTable;
    // End of variables declaration//GEN-END:variables

    
}
